package sms

import (
	"errors"
	"product/common/validator"
)

// ISmsSender 短信发送接口
type ISmsSender interface {
	// SendSms 发送短信
	SendSms(smsContent, mobile string) (bool, error)
	// BatchSendSms 批量发送短信
	BatchSendSms(smsContent string, mobiles []string) (bool, error)
}

// BaseSms 短信基础结构体
type BaseSms struct {
	// 用户名
	UserName string
	// 接口地址
	ApiUrl string
	// appKey
	AppKey string
}

// 检查短信配置是否合法
func (b *BaseSms) checkSmsConfigValid() (bool, error) {
	if validator.IsEmptyString(b.UserName) {
		return false, errors.New("用户名不能为空")
	}
	if validator.IsEmptyString(b.AppKey) {
		return false, errors.New("appKey不能为空")
	}
	if !validator.IsUrl(b.ApiUrl) {
		return false, errors.New("接口地址不合法")
	}
	return true, nil
}

// CheckInputValid BaseSms 方法
func (b *BaseSms) CheckInputValid(smsContent, mobile string) (bool, error) {
	valid, err := b.checkSmsConfigValid()
	if !valid {
		return false, err
	}
	if validator.IsEmptyString(smsContent) {
		return false, errors.New("短信内容不能为空")
	}
	if validator.IsEmptyString(mobile) || !validator.IsChineseMobile(mobile) {
		return false, errors.New("手机号不能为空或不合法")
	}

	return true, nil
}

// CheckBatchInputValid BaseSms 批量发送短信校检
func (b *BaseSms) CheckBatchInputValid(smsContent string, mobiles []string) (bool, error) {
	if len(mobiles) <= 0 {
		return false, errors.New("批量发送手机号码清单不能为空")
	}
	for _, mobile := range mobiles {
		valid, err := b.CheckInputValid(smsContent, mobile)
		if !valid {
			return false, err
		}
	}
	return true, nil
}
